package com.ssca.redis;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;

public class FtpConsumer {
	private static String IP = "192.168.1.184";
	private static String USER = "taseUser";
	private static String PWD = "62283748";

	public static void downloadFile(String remoteFilePath,String localPath){
		FTPClient ftpClient = new FTPClient();
		try {
			int reply;
			ftpClient.connect(IP);
			ftpClient.login(USER, PWD);
			reply=ftpClient.getReplyCode();

			//			ftpClient.changeWorkingDirectory(remotePath);
			ftpClient.setBufferSize(1024);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
			ftpClient.setControlEncoding("UTF-8");
			ftpClient.enterLocalPassiveMode();
			
			File remoteFile = new File(remoteFilePath);
			
			File localFile = new File(localPath+System.getProperty("file.separator")+remoteFile.getName());
			OutputStream os = new FileOutputStream(localFile);
			ftpClient.retrieveFile(remoteFilePath, os);
			os.close();
			System.out.println("success download:"+remoteFilePath);

		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				ftpClient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			} 
		}
	}

}
